package com.summer.peony.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.summer.peony.common.entity.AjaxResult;
import com.summer.peony.common.entity.AjaxResult.Type;
import com.summer.peony.system.pojo.SysMenu;
import com.summer.peony.system.service.SysMenuService;

@RestController
@RequestMapping("/system/menu")
public class SysMenuController {

	@Autowired
	private SysMenuService sysMenuService;

	/**
	 * @Description: TODO(新增菜单)
	 * @param sysMenu
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2021年1月6日 上午10:57:22
	 * @throws:
	 */
	@PostMapping("/add")
	public AjaxResult addMenu(@RequestBody SysMenu sysMenu) {
		AjaxResult ajaxResult = new AjaxResult();
		try {
			Integer add = sysMenuService.addMenu(sysMenu);
			if (add == 1) {
				ajaxResult.put("code", 0);
				ajaxResult.put("msg", "添加成功");
				return ajaxResult;
			}
			ajaxResult.put("code", 900007);
			ajaxResult.put("msg", "菜单编码已存在");
			return ajaxResult;
		} catch (Exception e) {
			e.printStackTrace();
			ajaxResult.put("code", 999999);
			ajaxResult.put("msg", "添加失败");
			return ajaxResult;
		}
	}

	/**
	 * @Description: TODO(修改菜单)
	 * @param sysMenu
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2021年1月6日 上午10:57:38
	 * @throws:
	 */
	@PutMapping("/up")
	public AjaxResult upMenu(@RequestBody SysMenu sysMenu) {
		Integer upd = sysMenuService.upMenu(sysMenu);
		AjaxResult ajaxResult = new AjaxResult();
		if (upd == 1) {
			ajaxResult.put("code", 0);
			ajaxResult.put("msg", "修改成功");
			return ajaxResult;
		}
		ajaxResult.put("code", 900008);
		ajaxResult.put("msg", "权限标识已存在");
		return ajaxResult;
	}

	/**
	 * @Description: TODO(删除菜单)
	 * @param menuId
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2021年1月6日 上午10:57:50
	 * @throws:
	 */
	@DeleteMapping("/del/{menuId}")
	public AjaxResult delMenu(@PathVariable("menuId") Integer menuId) {
		Integer del = sysMenuService.deleteByPrimaryKey(menuId);
		if (del == 1) {
			return new AjaxResult(Type.SUCCESS, "删除成功");
		} else if (del == 2) {
			return new AjaxResult(Type.ERROR, "有子级菜单不可删除");
		}
		return new AjaxResult(Type.ERROR, "删除失败");
	}

	/**
	 * @Description: TODO(菜单列表区分层级)
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2021年1月6日 上午10:58:02
	 * @throws:
	 */
	@GetMapping("/list")
	public AjaxResult menuList() {
		List<SysMenu> list = sysMenuService.menuList();
		if (list != null) {
			return new AjaxResult(Type.SUCCESS, "查询菜单列表", list);
		}
		return new AjaxResult(Type.SUCCESS, "查询数据为空");
	}

	/**
	 * @Description: TODO(查询所有菜单)
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2021年1月25日 上午10:41:04
	 * @throws:
	 */
	@GetMapping("/listAll")
	public AjaxResult menuListAll() {
		List<SysMenu> list = sysMenuService.menuListAll();
		if (list != null) {
			return new AjaxResult(Type.SUCCESS, "查询菜单列表", list);
		}
		return new AjaxResult(Type.SUCCESS, "查询数据为空");
	}

	/**
	 * @Description: TODO(查看菜单详情)
	 * @param menuId
	 * @return AjaxResult
	 * @author: TaoGe
	 * @date: 2021年1月6日 下午2:26:48
	 * @throws:
	 */
	@GetMapping("/id/{menuId}")
	public AjaxResult byIdMenu(Integer menuId) {
		SysMenu sysMenu = sysMenuService.byIdMenu(menuId);
		if (sysMenu != null) {
			return new AjaxResult(Type.SUCCESS, "查询菜单详情", sysMenu);
		}
		return new AjaxResult(Type.SUCCESS, "查询数据为空");
	}
}
